﻿using System;
using System.IO;
using System.Net.Mime;
using Company._4.Repository.Imp.Linq;
using Company.Model.Services;
using Company.Web.Infrastructure;
using StructureMap;
using StructureMap.Configuration.DSL;
using System.Data.Linq.Mapping;
using StructureMap.Attributes;

namespace Company.Web.Infrastructure
{
    public static class Bootstrapper
    {
        public static void StructureMapBD()
        {
            ObjectFactory.Configure(x =>
            {
                string m_Connection = ConfigurationCompany.GetConnectionString();
                x.ForRequestedType<BD>().CacheBy(InstanceScope.Hybrid).TheDefault.Is.ConstructedBy(() => new BD(m_Connection));
            });

        }

        public static void StructureMapBD(string p_mapping)
        {
            XmlMappingSource mappingSource;
            string m_fileName = System.Web.HttpContext.Current.Server.MapPath(@"Content\XML\");
            m_fileName += "map.xml";
            if (File.Exists(m_fileName))
            {
                string m_Connection = ConfigurationCompany.GetConnectionString();
                string m_file = File.ReadAllText(m_fileName).Replace("Shared", p_mapping.Trim());
                mappingSource = XmlMappingSource.FromXml(m_file);
                ObjectFactory.Configure(x =>
                {
                    x.ForRequestedType<BD>().CacheBy(InstanceScope.Hybrid).TheDefault.Is.ConstructedBy(() => new BD(m_Connection, mappingSource));
                });
            }
        }
    }

}




